//
//  PickerImagesAddedCollectionViewCell.swift
//  MiaoleGB
//
//  Created by ET|冰琳 on 16/7/22.
//  Copyright © 2016年 UOKO. All rights reserved.
//

import UIKit
import ReactiveSwift

/// 已添加的图片
class PickerImagesAddedCollectionViewCell: UICollectionViewCell {

    // 删除图片
    @IBOutlet weak var deleteButton: UIButton!
    
    //封面
    @IBOutlet weak var coverContainerView: UIView!
    
    @IBOutlet weak var progressView: UIView!
    
    @IBOutlet weak var imageView: UIImageView!
    
    @IBOutlet weak var reUploadButton: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.layer.cornerRadius = 4
    }
}

extension PickerImagesAddedCollectionViewCell{
    
    func bindWith(_ viewModel: PickerImageCellModel) {
        
        self.imageView.image = viewModel.entity.getThumnail()
        
        if let _ = viewModel.serverURL {
            self.progressView.isHidden = true
        }
        else {

            self.progressView.isHidden = false
            viewModel.progress.signal
                .take(until: self.reactive.prepareForReuse)
            .observe(on: UIScheduler())
            .observeValues({ [unowned self] (v) in
                
                guard let progress = v else {
                    
                    return
                }
                var originRect = self.progressView.frame
                
                originRect.size.height = self.height * (1 - progress)
                originRect.origin.y    = self.height - originRect.height
                self.progressView.frame = originRect
            })
        }
        
        viewModel.isUploadSuceed.signal.take(until: self.reactive.prepareForReuse)
            .observe(on: UIScheduler())
            .observeValues { [unowned self] (v) in
                self.reUploadButton.isHidden = v
            }
        
    }
}
